﻿using System;
using System.Data;
using System.Data.Common;
using WAD.Model;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Text;
using System.Collections.Generic;

namespace WAD.DAL
{
    public class DAOFCCash : DAO 
    {
        public static int Add(MFCCash m)
        {
            String sql = "insert into fc_cash ([user_id],orderid,amount,[platform],[state],record_id,openid,nickname,callback_msg,check_date,check_adminid,[date],wx_appid,wx_mchid) values(@user_id,@orderid,@amount,@platform,@state,@record_id,@openid,@nickname,@callback_msg,@check_date,@check_adminid,@date,@wx_appid,@wx_mchid)";
            Database db = GetDatabase();
            DbCommand cmd = db.GetSqlStringCommand(sql);
            db.AddInParameter(cmd, "@user_id", DbType.Int32, m.user_id);
            db.AddInParameter(cmd, "@orderid", DbType.String, m.orderid);
            db.AddInParameter(cmd, "@amount", DbType.Decimal, m.amount);
            db.AddInParameter(cmd, "@platform", DbType.Int32, m.platform);
            db.AddInParameter(cmd, "@state", DbType.Int32, m.state);
            db.AddInParameter(cmd, "@record_id", DbType.Int32, m.record_id);
            db.AddInParameter(cmd, "@openid", DbType.String, m.openid);
            db.AddInParameter(cmd, "@nickname", DbType.String, m.nickname);
            db.AddInParameter(cmd, "@callback_msg", DbType.String, m.callback_msg);
            db.AddInParameter(cmd, "@check_date", DbType.DateTime, m.check_date);
            db.AddInParameter(cmd, "@check_adminid", DbType.Int32, m.check_adminid);
            db.AddInParameter(cmd, "@date", DbType.DateTime, m.date);
            db.AddInParameter(cmd, "@wx_appid", DbType.String, m.wx_appid);
            db.AddInParameter(cmd, "@wx_mchid", DbType.String, m.wx_mchid);
            return db.ExecuteNonQuery(cmd);
        }

        public static PageData GetPageList(int pageIndex, int pageSize, string where, string fields)
        {
            return GetPageList(fields, "id desc", pageIndex, pageSize, where, "v_fc_cash#user");
        }

        public static List<Dictionary<string, object>> GetList(string where, string fields, int pageSize = 10, string tableName = "fc_cash")
        {
            if (string.IsNullOrEmpty(tableName)) tableName = "fc_cash";
            if (string.IsNullOrEmpty(where))
                return DAO.GetList(string.Format("select top {0} {1} from {2}", pageSize, fields, tableName));
            else
                return DAO.GetList(string.Format("select top {0} {1} from {2} where {3}", pageSize, fields, tableName, where));
        }

        public static int Delete(int user_id, string orderid)
        {
            return ExecuteNonQuery(string.Format("delete fc_cash where user_id={0} and orderid='{1}'", user_id, orderid));
        }

        /// <summary>
        /// 提现审核
        /// </summary>
        /// <param name="state">1审核通过</param>
        /// <param name="id"></param>
        /// <param name="adminid"></param>
        /// <returns></returns>
        public static int Check(int state, int id, int adminid, string callback_msg)
        {
            if (state == 1)
                return ExecuteNonQuery(string.Format("update fc_cash set state={0},check_adminid={1},check_date=getdate(),callback_msg='{2}' where id={3}", state, adminid, callback_msg, id));
            else if (state == 2)
                return ExecuteNonQuery(string.Format("update fc_cash set state={0},check_adminid={1},check_date=getdate() where id={2}", state, adminid, id));
            return 0;
        }

        /// <summary>
        /// 支付宝提现审核
        /// </summary>
        /// <param name="state"></param>
        /// <param name="orderid"></param>
        /// <param name="adminid"></param>
        /// <param name="callback_msg"></param>
        /// <param name="sys_msg"></param>
        /// <returns></returns>
        public static int Check_Ali_Fail(int state, string orderid, int adminid, string callback_msg, string sys_msg)
        {
            String sql = "update fc_cash set state=@state,check_adminid=@check_adminid,check_date=getdate(),callback_msg=@callback_msg,sys_msg=@sys_msg where orderid=@orderid";
            Database db = GetDatabase();
            DbCommand cmd = db.GetSqlStringCommand(sql);
            db.AddInParameter(cmd, "@state", DbType.Int32, state);
            db.AddInParameter(cmd, "@check_adminid", DbType.Int32, adminid);
            db.AddInParameter(cmd, "@callback_msg", DbType.String, callback_msg);
            db.AddInParameter(cmd, "@sys_msg", DbType.String, sys_msg);
            db.AddInParameter(cmd, "@orderid", DbType.String, orderid);
            return db.ExecuteNonQuery(cmd);
        }

        /// <summary>
        /// 发起审核后，设置回调状态(1 回调成功，2 等待回调)
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="state"></param>
        public static void SetCallback_State(string ids, int state)
        {
            ExecuteNonQuery(string.Format("update fc_cash set callback_state={0} where id in({1})", state, ids));
        }
    }
}